#ifndef MULTCHANNEL_H
#define MULTCHANNEL_H

#include "IStreamLoadableWritable.h"
#include "MultChannelHead.h"

class MultChannel : IStreamLoadableWritable
{
public:
    MultChannel() = default;
    ~MultChannel();
public:
    inline const std::vector<uint8_t*>& GetChannels() { return channels; }
    inline const MultChannelHead& GetMultChannelHead() { return multChannelHead; }
    inline void SetMultChannelHead(int x_, int y_, int w_, int h_, int channelCount_, MCType mcType_)
    {
        this -> multChannelHead = MultChannelHead(x_, y_, w_, h_, channelCount_, mcType_);
        for(uint8_t* cp : channels)
            free(cp);
        channels.clear();
        channels = std::vector<uint8_t*>(multChannelHead.GetChannelCount());
        int c_length = multChannelHead.GetH() * multChannelHead.GetW();
        for(uint8_t*& cp : channels)
        {
            cp = static_cast<uint8_t *>(malloc(c_length));
            memset(cp, 0, c_length);
        }
    }
    inline void SetMultChannel(int x_, int y_, int w_, int h_, const std::vector<uint8_t*>& cps_, MCType mcType_)
    {
        SetMultChannelHead(x_, y_, w_, h_, cps_.size(), mcType_);
        int c_length = w_ * h_;
        for(std::vector<uint8_t*>::size_type i = 0; i < cps_.size(); ++i)
            memcpy(channels[i], cps_[i], c_length);
    }
private:
    MultChannelHead multChannelHead;
    std::vector<uint8_t*> channels;
public:
    bool TryLoad(std::istream& i) final;
    bool TryDump(std::ostream& o) final;
    bool IsDumpable() final;

};


#endif //MULTCHANNEL_H
